<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2023-06-12
 * Time: 16:30
 */

namespace app\project\controller;

use app\common\controller\Common;
use app\project\service\AdvancePlanService;
use app\project\service\PublicService;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Overseer extends Controller
{

    //督查概况
    public function index(){
        if (!empty($_GET['type'])) {
            try{
                $pro_id = empty($_GET['pro_id'])?0:$_GET['pro_id'];
                $page = empty($_GET['page'])?1:$_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $where['pro_id'] = $pro_id;
                $returnBlack = PublicService::ListService("project_pro_overseer",$where,null,$page,$limit);
                $list = $returnBlack['list'];
                $statusArray = [0=>"待审核",1=>"已通过",2=>"已驳回"];
                foreach ($list as $k=>$v){
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                    unset($accountWhere);
                    if(!empty($v['add_id'])){
                        $accountWhere['id'] = $v['add_id'];
                        $sumbitName = Db::name("account")->where($accountWhere)->value("account");
                    }else{
                        $accountWhere['id'] = $v['account_id'];
                        $sumbitName = Db::name("project_account")->where($accountWhere)->value("name");
                    }
                    $list[$k]['sumbitName'] = $sumbitName;
                    $list[$k]['statusName'] = $statusArray[$v['status']];
                }
                $count = $returnBlack['count'];
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }else{
            //查询项目是否存在
            $id = $_REQUEST['id'];
            $common = new Common();
            $where['id'] = $id;
            $detail = $common->public_select("project_pro",$where)->find();


            //责任单位
            $Communal = new Communal();
            $duty_unit = $Communal->dictionary("project_dictionaries");
            $this->assign("duty_unit", json_encode($duty_unit));

            //责任人
            $accountWhere['uniacid'] = "1=1";
            $account = $common->public_select("project_account", $accountWhere, "id value,name title")->select();
            $this->assign("account", json_encode($account));


            $leader_v = explode(";", trim($detail['leader'], ";"));
            $leader_k = $this->takekey($account, $leader_v);

            $assign['leader_k'] = json_encode($leader_k);
            $assign['detail'] = $detail;


            $change_unit_v = explode(";", trim($detail['change_unit'], ";"));
            $change_k = $this->takekey($duty_unit, $change_unit_v);
            $assign['change_k'] = json_encode($change_k);

            $overseerArray = ['已督办','已预问责','已停职专抓','已移交纪委监委'];
            $assign['overseerArray'] = $overseerArray;

            $this->assign($assign);
            return view("index");
        }
    }

    //更新督查概况
    public function save(){
        $data = $this->request->post();
        $common = new Common();
        try {
            $id = $common->handle("project_pro", $data, 1);
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】更新概况,项目id【" . $id . "】";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        } catch (Exception $e) {
            Db::rollback();
            $black['code'] = 202;
            $black['line'] = $e->getLine();
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //添加完成情况视图
    public function detail(){
        $where['id'] = empty($_GET['id'])?0:$_GET['id'];
        $detail = PublicService::detailService("project_pro_overseer",$where);
        if(empty($detail['pro_id'])){
            $pro_id = empty($_GET['pro_id'])?0:$_GET['pro_id'];
            $detail['pro_id'] = $pro_id;
        }

        if(!empty($detail['images'])){
            $detail['images'] = explode(";",$detail['images']);
        }
        $assign['detail'] = $detail;
        $this->assign($assign);
        return view("detail");
    }


    //添加整个情况
    public function addOverseer(){
        $data = $this->request->post();
        $common = new Common();
        try {
            $rule = [
                ["content", "require", "内容不能为空"],
            ];
            $validate = new Validate($rule);
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }

            if(!empty($data['id'])){
                $where['id'] = $data['id'];
                $status = PublicService::findCounService("project_pro_overseer",$where,"status");
                if(!empty($status)){
                    throw new Exception("已审核，请勿重复审核操作");
                }
            }

            if(!empty($data['images'])){
                $data['images'] = implode(';',$data['images']);
            }
            if(!empty($data['status'])){
                $data['autoTime'] = time();
                if($data['status']==1){
                    $proId = $data['pro_id'];
                    $updateData['id'] = $proId;
                    $updateData['exposureAuto'] = 1;
                    PublicService::saveService("project_pro",$updateData);
                }
            }
            $id = $common->handle("project_pro_overseer", $data);
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】更新整改情况,关联id【" . $id . "】";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        } catch (Exception $e) {
            Db::rollback();
            $black['code'] = 202;
            $black['line'] = $e->getLine();
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    public function del(){
        try {
            $id = $_POST['id'];
            $where['id'] = ['in',$id];
            PublicService::delService("project_pro_overseer",$where);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    public function takekey($data, $value)
    {
        $black = [];
        foreach ($data as $k => $v) {
            if (in_array($v['title'], $value)) {
                $black[] = $v['value'];
            }
        }
        return $black;
    }
}